<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Support\Facades\DB;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
use App\Http\model\Cost;
use App\Http\Comm\CommonController as common;

class CostController extends Controller
{

	/**
	 * [serverList 服务列表]
	 * @Author   孙丽男
	 * @DateTime 2018-06-14
	 * @param    [type]
	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function serverList(Request $request){

		$cost = new Cost;
		$res = $cost->serverListModel();

		return $res;
	}

	/**
	 * [dic 添加页面字典数据]
	 * @Author   孙丽男
	 * @DateTime 2018-06-14
	 * @param    [type]
	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function dic(Request $request){
		$cost = new Cost;
		$res = $cost->dicModel();

		return $res;
	}

	/**
	 * [addCost 添加成本]
	 * @Author   孙丽男
	 * @DateTime 2018-06-14
	 * @param    [type]
	 * @version  6.2
	 * @param    Request    $request [description]
	 */
	public function addCost(Request $request){
		if ($request['sername']&&$request['name']&&$request['pattern']&&$request['settlement']&&$request['limitcar']&&$request['defaultcity']) {
			$data = $request->all();
			if (isset($data['s'])) {
                $data = array_except($data, ['s']);
            }
			$cost = new Cost;
			$res = $cost->addCostModel($data);
			return $res;

		}else{
			$result['code'] = 1002;
			$result['msg'] = '参数为空';
			return $result;
		}
	}
	/**
	 * [cityList 已关联城市列表]
	 * @Author   孙丽男
	 * @DateTime 2018-06-14
	 * @param    [type]
	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function cityList(Request $request){

		if ($request['costId']) {
			$cost = new Cost;
			$res = $cost->cityListModel($request['costId']);
			return $res;

		}else{
			$result['code'] = 1002;
			$result['msg'] = '参数为空';
			return $result;
		}
	}
	/**
	 * [relationCity 关联城市]
	 * @Author   孙丽男
	 * @DateTime 2018-06-14
	 * @param    [type]
	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function relationCity(Request $request){

		if ($request['costId']&&is_array($request['city'])) {
			$costId = $request->get('costId');
			$city = $request->get('city');
			$citystr = implode(',',$city);
			$cost = new Cost;
			$res = $cost->relationCityModel($costId ,$citystr);
			return $res;

		}else{
			$result['code'] = 1002;
			$result['msg'] = '参数为空';
			return $result;
		}
	}

	/**
	 * [relationCar 关联车系]
	 * @Author   孙丽男
	 * @DateTime 2018-06-14
	 * @param    [type]
	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function relationCar(Request $request){

		if ($request['serid']&&$request['costId']) {
			$cost = new Cost;
			$res = $cost->relationCarModel($request['serid'],$request['costId']);
			return $res;

		}else{
			$result['code'] = 1002;
			$result['msg'] = '参数为空';
			return $result;
		}
	}
	/**
	 * [relationAlgor 发布算法]
	 * @Author   孙丽男
	 * @DateTime 2018-06-14
	 * @param    [type]
	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function relationAlgor(Request $request){

		if ($request['costid'] && $request['name'] && $request['modtime'] && $request['algorithmid'] && $request['algorithmformula'] && $request['modpeo'] && $request['serid'] && $request['comment']) {
			$data = $request->all();
			if (isset($data['s'])) {
                $data = array_except($data, ['s']);
            }
			$cost = new Cost;
			$res = $cost->relationAlgorModel($data);
			return $res;

		}else{
			$result['code'] = 1002;
			$result['msg'] = '参数为空';
			return $result;
		}
	}

	/**
	 * [findCost 成本列表]
	 * @Author   孙丽男
	 * @DateTime 2018-06-14
	 * @param    [type]
	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function findCost(Request $request){
		//构建查询条件
    	$where = array();

        if ($request['costId']) {
            
            $where = [
                ['cost.id', $request['costId']],
            ]; 
        }

    	if ($request['start_time']) {
    		
    		$where[2] =  ['cost.modtime', '>',  $request['start_time']];
    	}

    	if ($request['end_time']) {
    		
    		$where[3] =  ['cost.modtime', '<',  $request['end_time']];
    	}

    	$cost = new Cost;
		$res = $cost->findCostModel($where);
		return $res;
	}

	/**
	 * [deleteCost 删除成本]
	 * @Author   孙丽男
	 * @DateTime 2018-06-14
	 * @param    [type]
	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function deleteCost(Request $request){

		if (is_array($request['costId'])) {
			$cost = new Cost;
			$res = $cost->deleteCostModel($request['costId']);
			return $res;

		}else{
			$result['code'] = 1002;
			$result['msg'] = '参数为空';
			return $result;
		}
	}

	/**
	 * [updateCost 修改成本]
	 * @Author   孙丽男
	 * @DateTime 2018-06-14
	 * @param    [type]
	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function updateCost(Request $request){

		if ($request['sername']&&$request['name']&&$request['pattern']&&$request['settlement']&&$request['limitcar']&&$request['defaultcity'] && $request['costId']) {
			$data = $request->all();
			if (isset($data['s'])) {
                $data = array_except($data, ['s']);
            }
			$costId = $request['costId'];
			unset($data['costId']);
			$cost = new Cost;
			$res = $cost->updateCostModel($data,$costId);
			return $res;

		}else{
			$result['code'] = 1002;
			$result['msg'] = '参数为空';
			return $result;
		}

	}

	/**
	 * [setStatus 冻结、启用]
	 * @Author   孙丽男
	 * @DateTime 2018-06-14
	 * @param    [type]
	 * @version  6.2
	 * @param    Request    $request [description]
	 */
	public function setStatus(Request $request){

		if ($request['costId']&&$request['serstatus']) {
			
			$cost = new Cost;
			$res = $cost->setStatusModel($request['costId'] ,$request['serstatus']);
			return $res;

		}else{
			$result['code'] = 1002;
			$result['msg'] = '参数为空';
			return $result;
		}
	}

	/**
	 * [findHistory 查看历史算法]
	 * @Author   孙丽男
	 * @DateTime 2018-06-14
	 * @param    [type]
	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function findHistory(Request $request){
		if ($request['costId']) {
			
			$cost = new Cost;
			$res = $cost->findHistoryModel($request['costId']);
			return $res;

		}else{
			$result['code'] = 1002;
			$result['msg'] = '参数为空';
			return $result;
		}
	}
}